__author__ = "carlos.c.saraiva"

import logging

from waveapi import events
from waveapi import model
from waveapi import robot
from waveapi import document

CMD_BUG = 'template:bug'
CMD_DISCUSS = 'template:discuss'
CMD_HELP = 'template:help'

HELP = '''
This Robot creates template headings to help organize discussions. Current options:

template: bug
template: discuss
template: help

'''
  
def OnBlipSubmitted(properties, context):
  """Invoked for every new Blip."""
  root_wavelet = context.GetRootWavelet()
  blip = context.GetBlipById(properties['blipId'])
  doc = blip.GetDocument()
#  HighlightBlip(doc)
  text = doc.GetText()
  rep_range = document.Range(0,len(text))
  if CMD_BUG in text:
    doc.SetTextInRange(rep_range, '\n\nWhat I was doing:\n\nWhat is wrong:\n\nWhat I expected:\n\n\n')
  if CMD_DISCUSS in text:
    doc.SetTextInRange(rep_range, '\n\nSubject:\n\nDiscussion:\n\nQuestions:\n\nDecisions:\n\n\n')
  if CMD_HELP in text:
    Notify(context, HELP)    

def OnRobotAdded(properties, context):
  """Invoked when the robot has been added."""
  Notify(context, HELP)
#  root_wavelet = context.GetRootWavelet()
#  root_wavelet.CreateBlip().GetDocument().SetText("hi there")


def Notify(context,message):
  root_wavelet = context.GetRootWavelet()
  root_wavelet.CreateBlip().GetDocument().SetText(message)

if __name__ == '__main__':
  myRobot = robot.Robot('template robot', 
      image_url='http://botemplate.appspot.com/assets/wave.png',
      version='1',
      profile_url='http://botemplate.appspot.com/')
#  myRobot.RegisterHandler(events.WAVELET_PARTICIPANTS_CHANGED, OnParticipantsChanged)
  myRobot.RegisterHandler(events.WAVELET_SELF_ADDED, OnRobotAdded)
  myRobot.RegisterHandler(events.BLIP_SUBMITTED, OnBlipSubmitted)
  myRobot.Run()
